package utils.test;

import beans.Task;
import engine.BIEngine0_1.TaskThread;
import net.TaskOperator;
import net.sf.json.JSONObject;
import utils.ExternalInterfaceParseUtil;
import java.io.*;
import java.sql.*;
import java.util.List;
import java.util.UUID;

public class SaveObject2DB {
    private static Connection conn;
    private PreparedStatement pres;

    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");              //加载驱动
            System.out.println("数据库加载成功!!!");
            String url="jdbc:mysql://localhost:3306/workflow?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
            String user="root";
            String password="root";

            conn=DriverManager.getConnection(url,user,password); //建立连接
            System.out.println("数据库连接成功!!!");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    public List<TaskThread> produceTaskThread(){
        String ndw = "{\n" +
                "    \"fatherTaskId\": \"121\",\n" +
                "    \"modelId\": \"250\",\n" +
                "    \"taskNumber\": \"2\",\n" +
                "    \"params\": [\n" +
                "        [\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_1\",\n" +
                "                \"param\": [\n" +
                "                    {\n" +
                "                        \"paramName\": \"p1\",\n" +
                "                        \"paramValue\": \"1_1_1\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"paramName\": \"p2\",\n" +
                "                        \"paramValue\": \"1_1_2\"\n" +
                "                    }\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_2\",\n" +
                "                \"param\": [\n" +
                "                    {\n" +
                "                        \"paramName\": \"p1\",\n" +
                "                        \"paramValue\": \"1_2_1\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"paramName\": \"p2\",\n" +
                "                        \"paramValue\": \"1_2_2\"\n" +
                "                    }\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_3\",\n" +
                "                \"param\": [\n" +
                "                    {\n" +
                "                        \"paramName\": \"-p0\",\n" +
                "                        \"paramValue\": \"1_2_1\"\n" +
                "                    }\n" +
                "                ]\n" +
                "            }\n" +
                "        ],\n" +
                "        [\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_1\",\n" +
                "                \"param\": [\n" +
                "                    {\n" +
                "                        \"paramName\": \"p1\",\n" +
                "                        \"paramValue\": \"1_1_1\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"paramName\": \"p2\",\n" +
                "                        \"paramValue\": \"1_1_2\"\n" +
                "                    }\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_2\",\n" +
                "                \"param\": [\n" +
                "                    {\n" +
                "                        \"paramName\": \"p1\",\n" +
                "                        \"paramValue\": \"1_2_1\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"paramName\": \"p2\",\n" +
                "                        \"paramValue\": \"1_2_2\"\n" +
                "                    }\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_3\",\n" +
                "                \"param\": [\n" +
                "                    {\n" +
                "                        \"paramName\": \"-p0\",\n" +
                "                        \"paramValue\": \"1_2_1\"\n" +
                "                    }\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    ],\n" +
                "    \"inputs\": [\n" +
                "        [\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_3\",\n" +
                "                \"blockName\": \"03:zipzip\",\n" +
                "                \"inputName\": \"-i0\",\n" +
                "                \"frontOutput\": {\n" +
                "                    \"frontOutputName\": \"b/b_a/b_a_b.jpg\",\n" +
                "                    \"blockId\": \"uiStart_1\",\n" +
                "                    \"dataId\": \"\"\n" +
                "                }\n" +
                "            }\n" +
                "        ],\n" +
                "        [\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_3\",\n" +
                "                \"blockName\": \"03:zipzip\",\n" +
                "                \"inputName\": \"-i0\",\n" +
                "                \"frontOutput\": {\n" +
                "                    \"frontOutputName\": \"b/b_a/b_a_a.jpg\",\n" +
                "                    \"blockId\": \"uiStart_1\",\n" +
                "                    \"dataId\": \"\"\n" +
                "                }\n" +
                "            }\n" +
                "        ]\n" +
                "    ],\n" +
                "    \"outputs\": [\n" +
                "        [\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_2\",\n" +
                "                \"blockName\": \"02:test5\",\n" +
                "                \"outputName\": \"o1\",\n" +
                "                \"outputAddress\": \"b/\",\n" +
                "                \"outputValue\": \".1\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_2\",\n" +
                "                \"blockName\": \"02:test5\",\n" +
                "                \"outputName\": \"o2\",\n" +
                "                \"outputAddress\": \"b/\",\n" +
                "                \"outputValue\": \".2\"\n" +
                "            }\n" +
                "        ],\n" +
                "        [\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_2\",\n" +
                "                \"blockName\": \"02:test5\",\n" +
                "                \"outputName\": \"o1\",\n" +
                "                \"outputAddress\": \"b/\",\n" +
                "                \"outputValue\": \".1\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blockId\": \"uiAlgorithm_2\",\n" +
                "                \"blockName\": \"02:test5\",\n" +
                "                \"outputName\": \"o2\",\n" +
                "                \"outputAddress\": \"b/\",\n" +
                "                \"outputValue\": \".2\"\n" +
                "            }\n" +
                "        ]\n" +
                "    ],\n" +
                "    \"data\": [\n" +
                "        [\n" +
                "            {\n" +
                "                \"dataBlockName\": \"外部数据源1\",\n" +
                "                \"dataName\": \"+uiData_1+1\",\n" +
                "                \"dataValue\": \"b/b_a/b_a_a.jpg\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"dataBlockName\": \"外部数据源2\",\n" +
                "                \"dataName\": \"+uiData_2+1\",\n" +
                "                \"dataValue\": \"b/b_a/b_a_b.jpg\"\n" +
                "            }\n" +
                "        ],\n" +
                "        [\n" +
                "            {\n" +
                "                \"dataBlockName\": \"外部数据源1\",\n" +
                "                \"dataName\": \"+uiData_1+1\",\n" +
                "                \"dataValue\": \"b/b_a/b_a_b.jpg\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"dataBlockName\": \"外部数据源2\",\n" +
                "                \"dataName\": \"+uiData_2+1\",\n" +
                "                \"dataValue\": \"b/b_a/b_a_a.jpg\"\n" +
                "            }\n" +
                "        ]\n" +
                "    ],\n" +
                "    \"other说明\": \"优先级和并行策略\",\n" +
                "    \"other\": [\n" +
                "        {\n" +
                "            \"Priority\": \"50\",\n" +
                "            \"ParallelStrategy\": \"-ut\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
//        ndw = ndw.replaceAll("\\+","%2B");
        JSONObject paramsJson = JSONObject.fromObject(ndw);
        String modelIdString = paramsJson.getString("modelId");
        String userId = "16";
        int modelIdInt = Integer.parseInt(modelIdString);
        ExternalInterfaceParseUtil externalInterfaceParseUtil = new ExternalInterfaceParseUtil();
        Task task = externalInterfaceParseUtil.createTaskByModelIdAndParamsJson(userId, modelIdInt,paramsJson);
        String taskHash = UUID.randomUUID().toString().substring(0,20);
        return new TaskOperator().produceTaskThread(task,taskHash);
    }

    public void saveTaskThread2DB(List<TaskThread> categoryList) {
        String sql = "update history_task set pause_info=? where task_hash=?";
        try {
            pres = conn.prepareStatement(sql);
            for (int i = 0; i < categoryList.size(); i++) {
                pres.setObject(1, categoryList.get(i));
                pres.setObject(2, categoryList.get(i).runningTaskBean.getTaskHash());
                pres.addBatch();
            }
            pres.executeBatch();                                      //批量插入到数据库中
            if(pres!=null)
                pres.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public TaskThread getTaskThread(String taskHash){
        TaskThread result = null;
        String sql="select pause_info from history_task where task_hash =?";
        try {
            pres=conn.prepareStatement(sql);
            pres.setString(1,taskHash);
            ResultSet res=pres.executeQuery();
            while(res.next()){
                Blob inBlob=res.getBlob(1);                             //获取blob对象

                InputStream is=inBlob.getBinaryStream();                //获取二进制流对象
                BufferedInputStream bis=new BufferedInputStream(is);    //带缓冲区的流对象
                byte[] buff=new byte[(int) inBlob.length()];
                while(-1!=(bis.read(buff, 0, buff.length))){            //一次性全部读到buff中
                    ObjectInputStream in=new ObjectInputStream(new ByteArrayInputStream(buff));
                    result =(TaskThread)in.readObject();                   //读出对象
                }
            }
        } catch (SQLException | IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) {
        SaveObject2DB saveObject2DB = new SaveObject2DB();
        List<TaskThread> taskThreadList = saveObject2DB.produceTaskThread();
        saveObject2DB.saveTaskThread2DB(taskThreadList);
        for(TaskThread taskThread:taskThreadList) {
            TaskThread one = saveObject2DB.getTaskThread(taskThread.runningTaskBean.getTaskHash());
            System.out.println(one.runningTaskBean.getTaskHash());
        }

    }
}
